package zpx.web;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class CheckCode {
	
	@RequestMapping(value="/SnowCheckCode",method=RequestMethod.GET)
	public String  uploadstar(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		//禁止缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0);
        // 指定生成的响应是图片
        response.setContentType("image/jpeg");
        int width = 166;        //验证码图片的宽度
        int height = 45;        //验证码图片的高度
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();       //获取Graphics类的对象
        //获取背景图片
        File bgImgFile = new File(request.getRealPath("images/checkCode_bg.jpg"));
        Image src = ImageIO.read(bgImgFile);  //构造Image对象
        g.drawImage(src, 0, 0, width, height, null);    //绘制背景图片
        /********************************************************/
        Random random = new Random();						//实例化一个Random对象
        g.setFont(new Font("宋体", Font.PLAIN, 16));	//设置字体
        g.setColor(new Color(255, 255, 255));     //设置雪花的颜色
        String sRand = "";
        Font mFont = new Font("宋体", Font.ITALIC, 26);       //通过Font构造字体
        g.setFont(mFont);   //设置字体
        g.setColor(new Color(0, 0, 0));     //设置颜色为黑色
        // 输出随机的验证码文字
        int itmp = 0;
        for (int i = 0; i < 4; i++) {
            if (random.nextInt(2) == 1) {
                itmp = random.nextInt(26) + 65;	//生成A~Z的字母
            } else {
                itmp = random.nextInt(10) + 48;	//生成0~9的数字
            }
            char ctmp = (char) itmp;
            sRand += String.valueOf(ctmp);
            /****随机缩放文字并将文字旋转指定角度**/
            //将文字旋转指定角度
            Graphics2D g2d_word = (Graphics2D) g;
            AffineTransform trans = new AffineTransform();
            //缩放文字
            float scaleSize = random.nextFloat() + 0.5f;
            if (scaleSize < 0.8 || scaleSize > 1.1f) {
                scaleSize = 1f;
            }
            trans.scale(scaleSize, scaleSize);
            g2d_word.setTransform(trans);
            /************************/
            g.drawString(String.valueOf(ctmp), width / 6 * i + 23, random.nextInt(height / 2) + 20);

        }
        // 将生成的验证码保存到Session中
        HttpSession session = request.getSession(true);
        session.setAttribute("randCheckCode", sRand);
        g.dispose();
        ImageIO.write(image, "JPEG", response.getOutputStream());
		return null;
	}
}
